package com.hudongwx.sadpw.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.GridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hudongwx.sadpw.MyGridView;
import com.hudongwx.sadpw.MyJsonObject;
import com.hudongwx.sadpw.R;
import com.hudongwx.sadpw.fragment.adapter.GridAdapter;
import com.hudongwx.sadpw.fragment.adapter.MyRecycleAdapter;
import com.hudongwx.sadpw.fragment.adapter.MyRecycleAdapter2;
import com.hudongwx.sadpw.utils.API;

import org.json.JSONArray;
import org.json.JSONException;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

/**
 *
 */
@ContentView(R.layout.fragment_home_page)
public class HomePageFragment extends Fragment {
    private View view;
    private String TAG="HomePageFragment";
    private MyAdapter myAdapter;
    JSONArray img,grid,recycle1,recycle2;
    @ViewInject(R.id.home_recycle)
    private RecyclerView recyclerView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if (null==view){
            view= x.view().inject(this,inflater,container);
            //获取一个管理器
            GridLayoutManager gridLayoutManager =new GridLayoutManager(getActivity(),1);
            recyclerView.setLayoutManager(gridLayoutManager);
            myAdapter=new MyAdapter(img,grid,recycle1,recycle2);
            recyclerView.setAdapter(myAdapter);
            parseAll();
        }
        return view;
    }
    //解析json
    public void parseAll(){
        parseGrid();
        parseCommunity();
        parseSpecial();
    }
    //解析6个数据
    public void parseGrid(){
        Log.i(TAG, "parseGrid: >>>>>>>>>>");
        RequestParams params = new RequestParams(API.getFoodMenu());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: 22222"+data.toString());
                    myAdapter.updateGrid(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: ex:"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    //解析社区热帖
    public void parseCommunity(){
        RequestParams params = new RequestParams(API.getCommunity());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    myAdapter.updateRecycle1(data);
                    Log.i(TAG, "onSuccess: 11111"+data.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: ex:"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    //解析精选专题
    public void parseSpecial(){
        RequestParams params=new RequestParams(API.getSpecial());
        x.http().get(params, new Callback.CommonCallback<MyJsonObject>() {
            @Override
            public void onSuccess(MyJsonObject result) {
                try {
                    JSONArray data = result.getJSONArray("Data");
                    Log.i(TAG, "onSuccess: ---------下载成功12121211"+data.length());
                    myAdapter.updateRecycle2(data);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.i(TAG, "onError: ex:"+ex);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
        JSONArray img,grid,list1,list2;
        public MyAdapter(JSONArray img,JSONArray grid,JSONArray list1,JSONArray list2) {
            this.img=img;
            this.grid=grid;
            this.list1=list1;
            this.list2=list2;
        }
        //更新数据
        public void updateViewPager(JSONArray img){
            this.img=img;
            notifyDataSetChanged();
        }
        public void updateGrid(JSONArray grid){
            this.grid=grid;
            notifyDataSetChanged();
        }
        public void updateRecycle1(JSONArray list1){
            this.list1=list1;
            notifyDataSetChanged();
        }
        public void updateRecycle2(JSONArray list2){
            this.list2=list2;
            notifyDataSetChanged();
        }

        //解析每个布局
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            MyViewHolder myViewHolder = null;
            View view;
            switch (viewType) {
                case 0:
                    view =getActivity().getLayoutInflater().inflate(R.layout.home_0, null);
                    myViewHolder=new MyViewHolder(view);
                    break;
                case 1:
                    Log.i(TAG, "onCreateViewHolder: grid:::::");
                    view = getActivity().getLayoutInflater().inflate(R.layout.home_1, null);
                    myViewHolder=new MyViewHolder(view);
                    break;
                case 2:
                    view = getActivity().getLayoutInflater().inflate(R.layout.home_2, null);
                    myViewHolder=new MyViewHolder(view);
                    break;
                case 3:
                    view = getActivity().getLayoutInflater().inflate(R.layout.home_3, null);
                    myViewHolder=new MyViewHolder(view);
                    break;
            }
            return myViewHolder;
        }

        //根据每个布局找到每个对应的控件
        public void onBindViewHolder(MyViewHolder holder, int position) {
            switch (position) {
                case 0:
                    break;
                case 1:
                    if (grid==null){
                        holder.gridView.setVisibility(View.GONE);
                    }else {
                        holder.gridView.setVisibility(View.VISIBLE);
                        holder.gridView.setAdapter(new GridAdapter(grid,getActivity()));
                    }
                    break;
                case 2:
                    if (null==list1){
                        holder.recyclyView1.setVisibility(View.GONE);

                    }else{
                        holder.recyclyView1.setVisibility(View.VISIBLE);
                        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
                        holder.recyclyView1.setLayoutManager(linearLayoutManager);
                        holder.recyclyView1.setAdapter(new MyRecycleAdapter(list1,getActivity()));
                    }

                    break;
                case 3:
                    if (list2==null){
                        holder.recyclyView2.setVisibility(View.GONE);
                    }else {
                        holder.recyclyView2.setVisibility(View.VISIBLE);
                        LinearLayoutManager linearLayoutManager1 = new LinearLayoutManager(getActivity());
                        holder.recyclyView2.setLayoutManager(linearLayoutManager1);
                        holder.recyclyView2.setAdapter(new MyRecycleAdapter2(list2,getActivity()));
                    }
                    break;
            }

        }

        @Override
        public int getItemViewType(int position) {
            return position;
        }

        @Override
        public int getItemCount() {
            return 4;
        }

    }
    class MyViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textView;
        ViewPager viewPager;
        MyGridView gridView;
        RecyclerView recyclyView1,recyclyView2;


        public MyViewHolder(View itemView) {
            super(itemView);
            viewPager = (ViewPager) itemView.findViewById(R.id.vp_home0);
            gridView = (MyGridView) itemView.findViewById(R.id.grid_home1);
            recyclyView1 = (RecyclerView) itemView.findViewById(R.id.rec_home_2);
            recyclyView2 = (RecyclerView) itemView.findViewById(R.id.rec_home_3);
        }
    }
}
